#!/bin/bash

# parameters for this script

## TEST indicates whether the below hyperparameters are the default values (test run)
##     or the actual values used for the paper, indicated below in comment#
##     this is neccesary for the figures scripts since model hyperparams for filenames are hard-coded
##     if using other values for hyperparameters, hardcoding in figures scripts must be modified manually
TEST=0 # 0 for full run

#############################################

# Global hyperparameters

## earliest congress number
export CONG_MIN=93 # 93
## latest congress number
export CONG_MAX=113 # 113

## number of chains
export CHAINS=1 # 4 (8 for 105th congress)

## number of burn-in
export BURN=15000 #15000

## number of iterations
export ITER=25000 #25000

## thinning factor
export THIN=3 #3

#############################################
# Run hierarchical model for all congresses
fldr="stage0"
stage=0 # used when running different versions of the mcmc model

# run mcmc for all congresses and chains
for ((cong=$CONG_MIN; cong<=$CONG_MAX; cong++))
do
for ((chain=1; chain<=$CHAINS; chain++))
do
	cong_str=`printf %03d $cong`
        fileid=H$cong_str'_'$chain
        echo $fileid
	Rscript 1_run_models.R $cong $chain $stage $BURN \
		$ITER $THIN $fldr & # launches processes in parallel
done
done

wait # wait for all background processes (mcmc runs) to finish

for ((cong=$CONG_MIN; cong<=$CONG_MAX; cong++))
do
	suffix=${BURN}_${ITER}_${THIN}
	Rscript 2_postprocess.R $cong $stage $fldr $suffix
done

#############################################
# Make figures in the main paper
Rscript make_data_figures.R
Rscript make_results_figures.R $BURN $ITER $THIN $fldr

#############################################
# Run model without covariates for all congresses
#    (this is needed for two figures in the supplement)
fldr="stage1"
stage=1 # used when running different versions of the mcmc model

# run mcmc for all congresses and chains
for ((cong=$CONG_MIN; cong<=$CONG_MAX; cong++))
do
for ((chain=1; chain<=$CHAINS; chain++))
do
    cong_str=`printf %03d $cong`
        fileid=H$cong_str'_'$chain
        echo $fileid
    Rscript 1_run_models.R $cong $chain $stage $BURN \
        $ITER $THIN $fldr & # launches processes in parallel
done
done

wait # wait for all background processes (mcmc runs) to finish

for ((cong=$CONG_MIN; cong<=$CONG_MAX; cong++))
do
    suffix=${BURN}_${ITER}_${THIN}
    Rscript 2_postprocess.R $cong $stage $fldr $suffix
done

#############################################
# Make figures in the supplement
Rscript make_supplement_figures.R $BURN $ITER $THIN





